/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EmployeeManage.java
 *
 * Created on Aug 22, 2011, 6:18:18 PM
 */
package Thanh;

import app.Global;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author ankhang
 */
public class EmployeeManage extends javax.swing.JPanel {

    Vector vColumn = new Vector();
    Vector vData = new Vector();

    /** Creates new form EmployeeManage */
    public EmployeeManage() {
        initComponents();
        vColumn.add("ID");
        vColumn.add("Username");
        vColumn.add("Fullname");
        vColumn.add("Department");
        vColumn.add("Role");
        vColumn.add("Phone");
        vColumn.add("Email");
        vColumn.add("Address");
        InitDepartmentSelect();
        ViewEmployee();
    }

    private void InitDepartmentSelect() {
        Connection cn = Global.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;

        cbbSelectDepartment.removeAll();
        try {
            ps = cn.prepareStatement("SELECT D.name"
                    + " FROM departments D"
                    + " WHERE D.active = 1");

            rs = ps.executeQuery();
            while (rs.next()) {
                cbbSelectDepartment.addItem(rs.getString(1));
            }
            tbl_EmployeeData.setModel(new DefaultTableModel(vData, vColumn));
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeManage.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                rs.close();
                ps.close();
                cn.close();
            } catch (SQLException ex) {
                Logger.getLogger(EmployeeManage.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void ViewEmployee() {
        Connection cn = Global.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        vData = new Vector();

        try {
            ps = cn.prepareStatement("SELECT E.id, E.username, E.first_name, E.last_name, D.name, R.name, E.mobile, E.email, E.address"
                    + " FROM employees E, departments D, rolls R"
                    + " WHERE E.active = 1 AND D.active = 1 AND D.id = E.department_id AND E.roll_id = R.id ORDER BY E.id");
            rs = ps.executeQuery();
            while (rs.next()) {
                Vector vRow = new Vector();
                vRow.add(rs.getInt(1));
                vRow.add(rs.getString(2));
                vRow.add(rs.getString(3) + ". " + rs.getString(4));
                vRow.add(rs.getString(5));
                vRow.add(rs.getString(6));
                vRow.add(rs.getString(7));
                vRow.add(rs.getString(8));
                vRow.add(rs.getString(9));
                vData.add(vRow);
            }   
            tbl_EmployeeData.setModel(new DefaultTableModel(vData, vColumn));
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeManage.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                rs.close();
                ps.close();
                cn.close();
            } catch (SQLException ex) {
                Logger.getLogger(EmployeeManage.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbl_EmployeeData = new javax.swing.JTable();
        cbbSelectDepartment = new javax.swing.JComboBox();
        btnUpdateProfile = new javax.swing.JButton();
        btnInsert = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(888, 600));
        setPreferredSize(new java.awt.Dimension(888, 600));

        tbl_EmployeeData.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbl_EmployeeData);

        cbbSelectDepartment.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        btnUpdateProfile.setText("Modify profile");
        btnUpdateProfile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateProfileActionPerformed(evt);
            }
        });

        btnInsert.setText("New Employee");
        btnInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInsertActionPerformed(evt);
            }
        });

        btnRefresh.setText("Refresh");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 868, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cbbSelectDepartment, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)
                        .addComponent(btnInsert)
                        .addGap(18, 18, 18)
                        .addComponent(btnUpdateProfile)
                        .addGap(26, 26, 26)
                        .addComponent(btnRefresh)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbbSelectDepartment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnInsert)
                    .addComponent(btnUpdateProfile, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnRefresh))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
                .addGap(25, 25, 25))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnUpdateProfileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateProfileActionPerformed
        // TODO add your handling code here:
        int row = tbl_EmployeeData.getSelectedRow();
        if(row==-1){
            JOptionPane.showMessageDialog(this, "No row selected !!!");
        } else {
            Vector result = (Vector)vData.get(row);
            int id = Integer.parseInt(result.get(0).toString());
            new ModifyProfile(Global.getFrame(), true, id).setVisible(true);
        }
    }//GEN-LAST:event_btnUpdateProfileActionPerformed

    private void btnInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInsertActionPerformed
        // TODO add your handling code here:
        new InsertEmployee(Global.getFrame(), true).setVisible(true);
    }//GEN-LAST:event_btnInsertActionPerformed

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        ViewEmployee();
    }//GEN-LAST:event_btnRefreshActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnInsert;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnUpdateProfile;
    private javax.swing.JComboBox cbbSelectDepartment;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbl_EmployeeData;
    // End of variables declaration//GEN-END:variables
}
